package ruifeng.zcj.communcationring.ui.circle;

import android.app.Activity;
import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.webkit.WebView;

import com.easemob.easeui.model.EaseVoiceRecorder;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.io.File;

import ruifeng.zcj.communcationring.base.base_web_activity.WebViewActivity;
import ruifeng.zcj.communcationring.base.base_web_activity.other.JS;
import ruifeng.zcj.communcationring.groble.FragmentArgs;
import ruifeng.zcj.communcationring.groble.Whole;
import ruifeng.zcj.communcationring.groble.utils.Validate;
import ruifeng.zcj.communcationring.ui.circle.bean.Voice;
import ruifeng.zcj.communcationring.ui.circle.other.CircleJS;
import ruifeng.zcj.communcationring.utils.Media;

/**
 * Created by Administrator on 2016/8/26 0026.
 */
public class CircleAcvitity2 extends WebViewActivity {
    private HttpUtils httpUtils;
    protected EaseVoiceRecorder voiceRecorder;

    public static void launch(Activity activity,
                              Class<? extends Fragment> clazz,
                              FragmentArgs args) {
        Intent intent = new Intent(activity, CircleAcvitity2.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getName());
        if (args != null) {
            intent.putExtra(KEY_ARGS, args);
        }
        activity.startActivity(intent);
    }

    @Override
    public void initView() {
        String className = getIntent().getStringExtra(KEY_CLASS_NAME);
        if (TextUtils.isEmpty(className)) {
            finish();
            return;
        }
        FragmentArgs values = (FragmentArgs) getIntent().getSerializableExtra(KEY_ARGS);
        MyURL = (String) values.get(CircleFragment.FILM_URL);

        voiceRecorder = new EaseVoiceRecorder(null);

        super.initView();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        mWebview = view;

        //将过滤到的url加入历史栈中
        return true;
    }

    // 是否调用js代码默认是传递
    @Override
    protected boolean isAddJavascriptInterface() {
        return true;
    }

    // new一个子类
    @Override
    public JS newJsObj() {// 传递一个子类给
        CircleJS aa = new CircleJS();
        aa.jsona = "zcj";
        return aa;
    }

    // 一个key
    @Override
    public String HTMLkey() {// 获取他的key
        return "Android";
    }

    public void stopRecord() {
        if (voiceRecorder.isRecording()) {
            voiceRecorder.discardRecording();
        }
        if (!Validate.isNullOrEmpty(voice)) {
            upataMp3(voice);
        }
    }

    public void startRecording() {
        if (voiceRecorder != null){
            voiceRecorder.startRecording(mContext);
        }
    }

    public Voice voice = null;

    // js代码调用java代码之后该做什么事情做什么事情
    public void pay(String arg) {
        if (arg.equals("stop")) {
           // Toast.makeText(App.getAppContext(), "停止录音", Toast.LENGTH_SHORT).show();
            stopRecord();
        } else {
            voice = Voice.parasjson(arg);
            if (!Validate.isNullOrEmpty(voice)) {
                if (voice.getOp().equals("start")) {
                   startRecording();
                }
            }
        }
    }

    public void finishActivity() {
        CircleAcvitity2.this.finish();
    }


    /**
     *
     * **/
    private void upataMp3(Voice voice) {
        File files = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            files = new File(voiceRecorder.getVoiceFilePath());
            if (!files.exists()) {
                files.mkdir();
            }
        }
        RequestParams paras = new RequestParams();
        paras.addBodyParameter("uploadfile", files);
        if (!Validate.isNullOrEmpty(voice)) {
            paras.addBodyParameter("id", voice.getId());
            paras.addBodyParameter("cid", voice.getCid());
        } else {
            paras.addBodyParameter("id", "服务器发送过来的数据有bug");
            paras.addBodyParameter("cid", "服务器发送过来的数据有bug");
        }

        httpUtils = new HttpUtils();
        String url = Whole.UplodataFile;

        httpUtils.send(HttpRequest.HttpMethod.POST, url, paras,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        // Toast.makeText(getApplicationContext(), "错误", Toast.LENGTH_SHORT).show();
                        System.out.println("zcj错误" + arg0.toString());
                        System.out.println("zcj错误" + arg1);


                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        super.onLoading(total, current, isUploading);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseinfo) {
//                        button3.setText("成功："+responseinfo.result);
                        mWebview.reload();
                    }
                });

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mWebview != null) {
            mWebview.removeAllViews();
            mWebview.destroy();
        }
    }
}
